// pages/packet/packet.js
var app = getApp();
Page({

  /**
   * 页面的初始数据 
   */
  data: {
    num:0,
    hbinfo:''
  },
  packetHandle:function(){
    wx.redirectTo({
      url: '/pages/packet2/packet2',
    })
  },
  inputHandle: function (e) {
    let that = this
    let { num } = this.data
    if (e.detail.value ){
      var money = e.detail.value
      
      this.setData({
        num: e.detail.value
      })
    }else{
      this.setData({
        num: 0
      })
    }
    
  },
  msginput:function(e){
    var that = this
    if(e.detail.value){
      that.setData({
        hbtext: e.detail.value
      })
    }else{
      that.setData({
        hbtext: ''
      })
    }
  },
  viewcard:function(e){
    var that = this;
    var requestdata = wx.getStorageSync('requestdata')
    var money = that.data.num
    requestdata.money = money
    var hbtext = that.data.hbtext
    requestdata.hbtext = hbtext?hbtext:'恭喜发财，大吉大利'
    wx.setStorageSync('requestdata',requestdata)
    wx.navigateTo({
      url: '/pages/viewcard/viewcard',
    })
  },
  makecard:function(){
    var that = this
    var money = that.data.num
    if (money !=0 && (money > 200 || money < 0.01)) {
      wx.showToast({
        title: '红包金额在0.01~200之间',
        icon: "none"
      })
      return;
    }
    var hbtext = that.data.hbtext
    if(!hbtext){
      hbtext = '恭喜发财，大吉大利'
    }
    var data = wx.getStorageSync('requestdata');
    data.money = that.data.num
    data.hbtext = hbtext
    data.hbtype = 1
    if (data.money>0){
      data.num = 1
    }else{
      data.num = 0
    }
    // console.log(data);return;
    wx.request({
      url: app.siteinfo.rootpath +'createhb',
      data:data,
      success:function(res){
        if(res.data.code == 200){
          if(data.money>0){
            var resid = res.data.id
            wx.request({
              url: app.siteinfo.rootpath + 'paymoney',
              data: {
                id: res.data.id
              },
              method: 'post',
              success: function (res) {
                console.log(res)
                wx.requestPayment({
                  'appId': res.data.appId,
                  'timeStamp': res.data.timeStamp,
                  'nonceStr': res.data.nonceStr,
                  'package': res.data.package,
                  'signType': res.data.signType,
                  'paySign': res.data.paySign,
                  'success': function (res) {
                    console.log(res);
                    wx.removeStorageSync('music')
                    wx.removeStorageSync('greet')
                    wx.removeStorageSync('requestdata')
                    wx.redirectTo({
                      url: '/pages/preview/preview?id=' + resid,
                    })
                  },
                  'fail': function (res) {
                    console.log(res);
                  },
                })
              }
            })
          }else{
            wx.removeStorageSync('music')
            wx.removeStorageSync('greet')
            wx.removeStorageSync('requestdata')
            wx.redirectTo({
              url: '/pages/preview/preview?id=' + res.data.id,
            })
          }

        }else{
          wx.showToast({
            title: '贺卡生成失败',
            icon:"none"
          })
        }
      }
    })
    // console.log(data)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.request({
      url: app.siteinfo.rootpath+'gethbinfo',
      method:"get",
      success:function(res){
        that.setData({
          hbinfo:res.data.hbinfo
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})